package com.qkl.xiatang.app.web.controller;

import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.dto.pond.AddPondGroupDTO;
import com.qkl.xiatang.app.entity.pond.PondEntity;
import com.qkl.xiatang.app.exception.BusinessException;
import com.qkl.xiatang.app.service.impl.PondManagerService;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class PondManageController {

    @Autowired
    private PondManagerService pondService;

    /**
     * 添加分组
     * @param webDTO
     * @return
     */
    @PostMapping("/addPondGroup")
    public Result addPondGroup(@RequestBody AddPondGroupDTO webDTO) {
        log.info("=======addPondGroup.webDTO={}",webDTO);
        String groupName = StringUtils.trimToEmpty(webDTO.getGroupName());
        List<String> pondIdList = webDTO.getPondIdList();
        if(StringUtils.isEmpty(groupName) || ObjectUtils.isEmpty(pondIdList)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        List<String> pondIds = AppUtils.stringsDistinct(pondIdList);
        if(ObjectUtils.isEmpty(pondIds)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        webDTO.setGroupName(groupName);
        webDTO.setPondIdList(pondIds);
        return ControllerHelper.toOperateResult(pondService.addPondGroup(webDTO));
    }

    /**
     * 查询塘(棚)列表
     * @return
     */
    @GetMapping("/queryPondList")
    public Result queryPondList(@RequestParam Map<String,Object> params) {
        String pondGroupId = StringUtils.trimToEmpty((String) params.get("pondGroupId"));
        String type = StringUtils.trimToEmpty((String) params.get("type"));
        log.info("====queryPondList.pondGroupId={},type={}",pondGroupId,type);
        return ControllerHelper.toQueryResult(pondService.queryPondList(pondGroupId,AppUtils.parseInt(type)));
    }

    /**
     * 查询塘(棚)分组列表
     * @return
     */
    @GetMapping("/queryPondGroupList")
    public Result queryPondGroupList(@RequestParam Map<String,Object> params) {
        String type = StringUtils.trimToEmpty((String) params.get("type"));
        log.info("====queryPondGroupList.type={}",type);
        return ControllerHelper.toQueryResult(pondService.queryPondGroupList(AppUtils.parseInt(type)));
    }

    /**
     *删除塘(棚)分组
     * @param params
     * @return
     */
    @PostMapping("/deletePondGroup")
    public Result deletePondGroup(@RequestBody Map<String,Object> params) {
        String pondGroupId = StringUtils.trimToEmpty((String)params.get("pondGroupId"));
        if(StringUtils.isEmpty(pondGroupId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toOperateResult(pondService.deletePondGroup(pondGroupId));
    }

    /**
     * 编辑塘(棚)分组
     * @param params
     * @return
     */
    @PostMapping("/editPondGroup")
    public Result editPondGroup(@RequestBody Map<String,Object> params) {
        String pondGroupId = StringUtils.trimToEmpty((String) params.get("pondGroupId"));
        String groupName = StringUtils.trimToEmpty((String) params.get("groupName"));
        List<String> pondIdList = (List<String>) params.get("pondIdList");
        pondIdList = AppUtils.stringsDistinct(pondIdList);

        if ( StringUtils.isEmpty(pondGroupId) || ( StringUtils.isEmpty(groupName) && ObjectUtils.isEmpty(pondIdList)) ) {
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }

        try{
            return ControllerHelper.toOperateResult(pondService.editPondGroup(pondGroupId,groupName,pondIdList));
        }catch (BusinessException e){
            return Result.error(ErrorCodeUtils.OPERATE_FAIL,e.getMessage());
        }catch (Exception e1){
            e1.printStackTrace();
            return Result.error(ErrorCodeUtils.OPERATE_FAIL,e1.getMessage());
        }
    }

    /**
     * 塘口信息查询
     * @param params
     * @return
     */
    @GetMapping("/queryPondInfo")
    public Result queryPondInfo(@RequestParam Map<String,Object> params) {
        String pondId = StringUtils.trimToEmpty((String) params.get("pondId"));
        if(StringUtils.isEmpty(pondId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        Object entity = pondService.queryPondById(pondId);
        if(null == entity){
            return Result.error(ErrorCodeUtils.QUERY_NOT_DATA, ErrorCodeUtils.QUERY_NOT_DATA_MSG);
        }
        return Result.success(entity,"塘口信息查询");
    }

    /**
     * 全部塘口列表查询
     * @return
     */
    @GetMapping("/queryAllPondList")
    public Result queryAllPondList() {
        List<PondEntity> entityList = pondService.queryAllPondList();
        if(ObjectUtils.isEmpty(entityList)){
            entityList = new ArrayList<>();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("list",entityList);
        return Result.success(map,"全部塘口列表查询");
    }

    /**
     * 搜索塘列表
     * @param params
     * @return
     */
    @GetMapping("/searchPondList")
    public Result searchPondList(@RequestParam Map<String,Object> params) {
        String keywords = (String) params.get("keywords");
        return ControllerHelper.toQueryResult(pondService.searchPondList(keywords));
    }

}
